//
//  SGCorePayment+UnionPay.m
//  smartGas
//
//  Created by chen on 2017/11/20.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "SGCorePayment+UnionPay.h"

#import "UPPaymentControl.h"
#import "YJHTTP.h"



@implementation SGCorePayment (UnionPay)


+ (void)unionPaymentTestWithViewController:(UIViewController *)controller{
    
    AFHTTPSessionManager *session = [[AFHTTPSessionManager alloc]init];
    session.responseSerializer  = [AFHTTPResponseSerializer serializer];
    
    [session GET:kUnionPayURL_TN_Normal
      parameters:nil
        progress:nil
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         NSLog(@"%@",responseObject);
         NSString* tn = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
         if (tn != nil && tn.length > 0)
         {
             [self startPaymentWithTN:tn viewController:controller];
             NSLog(@"tn=%@",tn);
         }
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         NSLog(@"%@",error);
     }];
}


+ (void)startPaymentWithTN:(NSString *)tn
            viewController:(UIViewController *)controller{
    /**
     * 参数
     * 1.流水号:商户后台向银联后台提交订单信息后，由银联后台生成并下发给商户后台的交易凭证；
     * 2.商户自定义协议，商户在调用支付接口完成支付后，用于引导支付控件返回而定义的协议，具体请参考4.4第二步中URL Type定义;
     3.接入模式，标识商户以何种方式调用支付控件，该参数提供以下两个可选值：
     "00"代表接入生产环境（正式版本需要）；
     "01"代表接入开发测试环境（测试版本需要）；
     */
    
    [[UPPaymentControl defaultControl] startPay:tn
                                     fromScheme:kPaymentAppScheme
                                           mode:kUnionPayMode_Development
                                 viewController:controller];
    
    /**
     1.支付结果url，传入后由SDK解析并通过completionBlock回调商户客户端；
     2.商户客户端定义的结果处理方法，此方法包行code、data两个传入参数，本文档4.5第三部分将详细说明；
     */
    //解析方法: 放到这里。
    //    [[UPPaymentControl defaultControl] handlePaymentResult:nil
    //                                             completeBlock:^(NSString *code, NSDictionary *data)
    //     {
    //
    //         NSLog(@"%@",code);
    //
    //     }];
}


//银联生成订单
+ (void)unionPayGeneratePaymentWithUserId:(NSString *)userId
                             amount:(NSString *)amount
                               type:(SGCorePaymentType)paymentType
                          goodsType:(SGCorePaymentGoodsType)goosType
                         relationId:(NSString *)relationId
                            success:(void(^)(NSString *paymentTN,NSString *des))success
                             failed:(void(^)(NSError *error, NSString *des))failed
{
    
    NSString *url = [NSString stringWithFormat:@"%@/busi/payment/payment",
                     HOST_Progress];
    
    NSDictionary *parameters = @{
                                 @"userId":StringValue(userId),
                                 @"payWay":@"1",//微信支付
                                 @"amount":StringValue(amount),
                                 @"type":[NSString stringWithFormat:@"%@",@(paymentType)],
                                 @"goodsType":[NSString stringWithFormat:@"%@",@(goosType)],
                                 @"relationId":StringValue(relationId),
                                 };
    
    [HTTP POST:url
    parameters:parameters
       success:^(id responseObject, NSString *des)
     {
         
         if (success) {
             success(responseObject[@"bring"],des);
         }
         
     } failure:^(NSError *error, NSString *des) {
         if (failed) {
             failed(error,des);
         }
     }];
}
@end
